<template>
  <div>
    <div class="goods">
      <!-- 左侧菜单栏 -->
      <div class="menu-wrapper" ref="menuWrapper">
        <ul>
          <!-- 每一个菜单文字和图标 -->
          <li
            class="menu-item"
            v-for="(item,index) in goods"
            :key="index"
            :class="{'current':currentIndex === index}"
            @click="selectMenu(index,$event)"
            ref="menuList"
          >
            <span class="text">
              <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>
              {{item.name}}
            </span>
          </li>
        </ul>
      </div>
      <!-- 右侧食物栏 -->
      <div class="foods-wrapper" ref="foodsWrapper">
        <ul>
          <li class="food-list food-list-hook" v-for="(item,index) in goods" :key="index">
            <h1 class="title">{{item.name}}</h1>
            <ul>
              <li
                class="food-item"
                v-for="(food,index) in item.foods"
                :key="index"
                @click="selectFood(food)"
              >
                <div class="icon">
                  <img :src="food.icon" alt width="57" height="57" />
                </div>
                <div class="content">
                  <h2 class="name">{{food.name}}</h2>
                  <p class="desc">{{food.description}}</p>
                  <div class="extra">
                    <span class="count">月售{{food.sellCount}}</span>
                    <span>好评率{{food.rating}}</span>
                  </div>
                  <div class="price">
                    <span class="now">¥{{food.price}}</span>
                    <span class="old" v-if="food.oldPrice">¥{{food.oldPrice}}</span>
                  </div>
                  <div class="cartcontrol-wrapper">
                    <cartcontrol :food="food"></cartcontrol>
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <!-- 购物车底部 -->
      <shopcart
        :selectFoods="selectFoods"
        :deliveryPrice="seller.deliveryPrice"
        :min-price="seller.minPrice"
      ></shopcart>
    </div>
    <!-- 商品详情 -->
    <food :food="selectedFood" ref="food"></food>
  </div>
</template>
<script>
import BScroll from "better-scroll"; // 引入betterscroll
import shopcart from "../shopcart/shopcart"; // 引入购物车组件
import cartcontrol from "../cartcontrol/cartcontrol"; // 引入增减控件
import food from "../food/food"; // 引入商品详情页组件

export default {
  name: "goods",
  props: {
    seller: {
      type: Object
    }
  },
  components: {
    shopcart, // 底部购物车
    cartcontrol, // 增减按钮
    food // 商品详情
  },
  data() {
    return {
      goods: [], // 请求接口的所有数据
      classMap: [], // 图标的类名
      listHeight: [], // 右侧food每个li距离顶部的高度,第一个是0
      scrollY: 0, // 右侧food滚动的高度
      selectedFood: {} // 点击的商品
    };
  },
  computed: {
    // 计算当前索引值(右侧滚动)
    currentIndex() {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        // 判断当前滚动位于哪两个区间之间 或当滚动到最后一个时候，height2不存在
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          // 滚动右侧，左侧滚动
          this._followScroll(i);
          return i;
        }
      }
      // return 0;
    },
    // 选中商品的数
    selectFoods() {
      let foods = [];
      this.goods.forEach(good => {
        good.foods.forEach(food => {
          if (food.count) {
            foods.push(food);
          }
        });
      });
      return foods;
    }
  },
  methods: {
    // 初始化滚动条
    _initScroll() {
      // 左侧菜单栏的滚动条
      this.menuScroll = new BScroll(this.$refs.menuWrapper, {
        click: true,
        probeType: 3,
        mouseWheel: true
      });
      // 右侧food滚动条
      this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {
        click: true,
        probeType: 3, // 滚动时候触发,监听滚动的位置
        mouseWheel: true
      });
      // 滚动时触发监听滚动的位置
      this.foodsScroll.on("scroll", pos => {
        this.scrollY = Math.abs(Math.round(pos.y));
      });
    },
    // 计算dom高度
    _calculateHeight() {
      // 获取右侧food的每个li dom 是个数组
      let foodList = this.$refs.foodsWrapper.getElementsByClassName(
        "food-list-hook"
      );
      let height = 0; // 初始化一个height
      this.listHeight.push(height);
      // 遍历这个数组，获取每个li的高度，并且累加，push进数组里
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i];
        height += item.clientHeight;
        this.listHeight.push(height);
      }
      // console.log(this.listHeight)
    },
    // 点击左侧，联动右侧
    selectMenu(index, event) {
      if (!event._constructed) {
        return;
      }
      // 根据index找到点击的对应右侧的区间,foodList是每一个li
      let foodList = this.$refs.foodsWrapper.getElementsByClassName(
        "food-list-hook"
      );
      let el = foodList[index];
      this.foodsScroll.scrollToElement(el, 800); // 调用betterScrollToElement的方法
    },
    // 滚动右侧，左侧联动
    _followScroll(index) {
      let menuList = this.$refs.menuList;
      let el = menuList[index];
      this.menuScroll.scrollToElement(el, 300);
    },
    // 点击查看商品详情
    selectFood(food) {
      this.selectedFood = food;
      this.$refs.food.show(); // 父组件调用子组件里的方法
    }
  },
  created() {
    // 图标的类名
    this.classMap = ["decrease", "discount", "special", "invoice", "guarantee"];
    // 获取右侧商品数据信息
    this.$http.get("/api/goods").then(res => {
      if (res.body.errno === 0) {
        let data = res.data.data;
        this.goods = data;
        console.log('右侧商品信息',this.goods);
        // 等dom生成后，初始化滚动条，计算高度
        this.$nextTick(() => {
          this._initScroll(); // 初始化页面滚动条
          this._calculateHeight(); // 计算右侧food的li的高度
        });
      }
    });
  }
};
</script>

<style lang='stylus' scoped>
.goods {
  display: flex;
  position: absolute;
  top: 174px;
  bottom: 46px;
  width: 100%;
  overflow: hidden;

  // 左侧的菜单栏
  .menu-wrapper {
    flex: 0 0 80px; // 三个值是
    width: 80px;
    background-color: #f3f5f7;

    .menu-item {
      display: table;
      height: 54px;
      width: 56px;
      padding: 0 12px;
      line-height: 14px;
      text-align: left;

      // 图标
      .icon {
        display: inline-block;
        vertical-align: top;
        width: 12px;
        height: 12px;
        margin-right: 2px;
        background-size: 12px 12px;
        background-repeat: no-repeat;

        &.decrease {
          vertical-align: top;
          background-image: url('./decrease_3@3x.png');
        }

        &.discount {
          vertical-align: top;
          background-image: url('./discount_3@3x.png');
        }

        &.guarantee {
          vertical-align: top;
          background-image: url('./guarantee_3@3x.png');
        }

        &.invoice {
          vertical-align: top;
          background-image: url('./invoice_3@3x.png');
        }

        &.special {
          vertical-align: top;
          background-image: url('./special_3@3x.png');
        }
      }

      // 文字
      .text {
        display: table-cell;
        width: 56px;
        width: 56px;
        vertical-align: middle;
        font-size: 12px;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      }

      &:last-child .text {
        border-bottom: none;
      }
    }

    // 滚动后左侧区间对应的样式
    .current {
      // position: relative;
      // z-index: 10;
      background-color: #fff;
      margin-top: -1px;
      font-weight: 700;

      .text {
        border: none;
      }
    }
  }

  // 右侧的食物栏
  .foods-wrapper {
    flex: 1;

    // 标题
    .title {
      padding-left: 14px;
      height: 26px;
      line-height: 26px;
      font-size: 12px;
      border-left: 2px solid d9dde1;
      background: #f3f5f7;
      color: rgb(147, 153, 159);
    }

    // 每一个食物的信息
    .food-item {
      display: flex;
      margin: 18px;
      padding-bottom: 18px;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);

      &:last-child {
        border-bottom: none;
        margin-bottom: 0;
      }

      // 图片
      .icon {
        flex: 0 0 57px;
        margin-right: 10px;
      }

      // 食物的信息
      .content {
        flex: 1;
        position: relative;

        .name {
          font-size: 14px;
          line-height: 14px;
          margin: 2px 0;
          color: rgb(7, 17, 27);
        }

        .desc, .extra {
          color: rgb(147, 153, 159);
          line-height: 10px;
          font-size: 12px;
        }

        .desc {
          line-height: 12px;
          margin-bottom: 8px;
        }

        // 销量
        .extra {
          .count {
            margin-right: 12px;
          }
        }

        // 价格
        .price {
          font-weigth: 700;
          line-height: 24px;

          // 新价格
          .now {
            // display inline-block;
            font-size: 14px;
            margin-right: 8px;
            // height: 30px;
            // line-height: 30px;
            color: rgb(240, 20, 20);
          }

          // 老价格
          .old {
            text-decoration: line-through;
            font-size: 10px;
            font-weight: 700;
            color: rgb(147, 153, 159);
          }
        }

        // 增减数量按钮
        .cartcontrol-wrapper {
          position: absolute;
          right: 0;
          bottom: -5px; // 12px;
        }
      }
    }
  }
}
</style>
